# Goal: Create Figures A3 & A4 
# by Jennifer Pan and Yiqing Xu

## Plotting

rm(list=ls(all=TRUE))


##########################################
## Figure A3 (Sample 1)
##########################################

library(ggplot2)
library(gridExtra)
d <- haven::read_dta("data/sample1.dta")
names(d)

## additive vs. PCA vs. IRT
dims <- c("nati","poli","econ","trad","equi","ethn")
for (i in 1:length(dims)) {
  iss <- dims[i]
  x1 <- paste0("index_",iss)
  x2 <- paste0("pca_",iss)
  x3 <- paste0("irt_",iss)   
  # index and pca
  corr <- cor(d[,x1], d[,x2])
  p1 <- ggplot(d, aes_string(x1, x2)) + geom_point(colour = "gray50") + geom_smooth(method = "gam") + 
    theme_bw() + xlim(-4,4) + ylim(-4,4) + xlab("Additive Index") + ylab("Principal Component") + 
    annotate(geom="text", x=0, y=3.8, label=paste0("Corr = ",sprintf(corr, fmt = '%#.2f')), size = 5)
  # index and irt
  corr <- cor(d[,x1], d[,x3])
  p2 <- ggplot(d, aes_string(x1, x3)) + geom_point(colour = "gray50") + geom_smooth(method = "gam") + 
    theme_bw() + xlim(-4,4) + ylim(-4,4) + xlab("Additive Index") + ylab("Item Response Theory") + 
    annotate(geom="text", x=0, y=3.8, label=paste0("Corr = ",sprintf(corr, fmt = '%#.2f')), size = 5)
  # pca and irt
  corr <- cor(d[,x2], d[,x3])
  p3 <- ggplot(d, aes_string(x2, x3)) + geom_point(colour = "gray50") + geom_smooth(method = "gam") + 
    theme_bw() + xlim(-4,4) + ylim(-4,4) + xlab("Principal Component") + ylab("Item Response Theory") + 
    annotate(geom="text", x=0, y=3.8, label=paste0("Corr = ",sprintf(corr, fmt = '%#.2f')), size = 5)
  # save
  pdf(paste0("graphs/measures_q_",iss,".pdf"), width = 10, height = 3.3)
  grid.arrange(p1, p2, p3, nrow = 1)
  graphics.off()  
}


##########################################
## Figure A4 (Sample 2)
##########################################

library(ggplot2)
library(gridExtra)
d <- haven::read_dta("data/sample2.dta")
names(d)

## additive vs. PCA vs. IRT
dims <- c("poli","econ","nati","trad","equi","ethn")
for (i in 1:length(dims)) {
  iss <- dims[i]
  x1 <- paste0("index_",iss)
  x2 <- paste0("pca_",iss)
  x3 <- paste0("irt_",iss)   
  # index and pca
  corr <- cor(d[,x1], d[,x2])
  p1 <- ggplot(d, aes_string(x1, x2)) + geom_point(colour = "gray50") + geom_smooth(method = "gam") + 
    theme_bw() + xlim(-4,4) + ylim(-4,4) + xlab("Additive Index") + ylab("Principal Component") + 
    annotate(geom="text", x=0, y=3.8, label=paste0("Corr = ",sprintf(corr, fmt = '%#.2f')), size = 5)
  # index and irt
  corr <- cor(d[,x1], d[,x3])
  p2 <- ggplot(d, aes_string(x1, x3)) + geom_point(colour = "gray50") + geom_smooth(method = "gam") + 
    theme_bw() + xlim(-4,4) + ylim(-4,4) + xlab("Additive Index") + ylab("Item Response Theory") + 
    annotate(geom="text", x=0, y=3.8, label=paste0("Corr = ",sprintf(corr, fmt = '%#.2f')), size = 5)
  # pca and irt
  corr <- cor(d[,x2], d[,x3])
  p3 <- ggplot(d, aes_string(x2, x3)) + geom_point(colour = "gray50") + geom_smooth(method = "gam") + 
    theme_bw() + xlim(-4,4) + ylim(-4,4) + xlab("Principal Component") + ylab("Item Response Theory") + 
    annotate(geom="text", x=0, y=3.8, label=paste0("Corr = ",sprintf(corr, fmt = '%#.2f')), size = 5)
  # save
  pdf(paste0("graphs/measures_s_",iss,".pdf"), width = 10, height = 3.3)
  grid.arrange(p1, p2, p3, nrow = 1)
  graphics.off()  
}


